
#pragma once
#include <d3dx9.h>
#include "goodlinklist.h"
#include <TextureItem.h>
#include "Character_define.h"

class CSkelMeshNode;
class CSkelMeshNodeInstance;
class _Bone;
class _BoneNode;
class _Anim;
class _Frame;
class _FrameNode;
class CModel;
class _ModelRCDes;

class CSkeletonMesh : public CGLLNode
{
	friend class CModel;
	friend class CModelRender;
private:
	CSkeletonMesh();
public:
	CSkeletonMesh(CModel* pModel,const int& iType = _SMESH_GENERAL,const int& iIndex = 0 );
	virtual ~CSkeletonMesh();

	BOOL InitBone();
	BOOL ResetBone();

	CSkelMeshNode* AddMesh(const _ModelRCDes* pDes);
	CSkelMeshNode* AddMesh(const int& iPartType,const char* pszSkinName,const char* pszTexName);
	BOOL SetAnim(const _ModelRCDes* pDes,const _AniPlayOption& playOpt,const BOOL& bReset = TRUE);

	BOOL ChangeMesh(const int& iPartType,const int& iIndex,const BOOL& bAdd);
	BOOL ChangeMesh(const int& iPartType,const char* pszSkinName,const char* pszTexName,const BOOL& bAdd);

	inline unsigned int GetMeshCount() const
	{
		return m_listMesh.GetCount();
	}

	CSkelMeshNode* GetMeshNodeByType(const int& iPartType);

	inline D3DXMATRIX* GetMatCombine(const int& iIndex) const
	{
		return &m_pmatCombine[iIndex];
	}

	inline void SetMatCombine(const int& iIndex,const D3DXMATRIX& mat)
	{
		m_pmatCombine[iIndex] = mat;
	}

	D3DXMATRIX* GetMatCombine(const char* szBoneNodeName) ;

	const uint32 GetBoneCount();
	void GetBoneWorldMat(D3DXMATRIX& mat,const uint32 uBoneID);

	int GetType() const
	{
		return m_iType;
	}

	void SetDistance(float fDis);
	float GetDistance() const;

	CModel*  GetParentModel()
	{
		return m_pParentModel;
	}

	BOOL SetEventFlag(const _AniEventType& eType,const _AniEventFLag& eFlag);

protected:
		void CheckFrame(const float& fTime);
		void Update();
		void UpdateShadow();
		void SetParentWorldMat();
private:
	void Bone2Frame(_FrameNode* pFram,_BoneNode* pBone,_BoneNode* pParentBone = NULL);
	float* GetBottom();//render used
	void AddMesh(const int& iPartType,CSkelMeshNode* pMesh);
	void Term();

protected:
	CGLinkedList<CSkelMeshNodeInstance*> m_listMesh;
	_Frame*                  m_pFram;
	_Bone*                   m_pBone;
	int                      m_iType;
	int                      m_iIndex;

	D3DXMATRIX*              m_pmatCombine;
private:
	CModel*                  m_pParentModel;
	D3DXMATRIX*              m_pMatParentWorld;
	D3DXMATRIX*              m_pMatParentShadow;

};

